﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace IMLibrary.BinaryPacket
{
    /// <summary>
    /// 二进制协议包
    /// </summary>
    public class BasePacket
    {
        #region 构造
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="HeaderLength"></param>
        public BasePacket(int HeaderLength)
        { 
            BaseData = new byte[HeaderLength];
        }

        /// <summary>
        /// 构造（将字节数组转换为协议对像）
        /// </summary>
        /// <param name="data">要生成包的数据</param>
        /// <param name="HeaderLength"></param>
        public BasePacket(byte[] data, int HeaderLength)
        {
            BaseData = data.Take(HeaderLength).ToArray();
            Data = data.Skip(HeaderLength).ToArray();
        }
        #endregion

        #region 变量
        /// <summary>
        /// 数据块
        /// </summary>
        private byte[] Data;
        /// <summary>
        /// 消息包头
        /// </summary>
        protected byte[] BaseData; 
        #endregion

        #region 获取包头数据
        /// <summary>
        /// 获取包头数据
        /// </summary>
        public byte[] Header
        {
            get { return BaseData; }
        }
        #endregion

        #region 获取有效数据块长度
        /// <summary>
        /// 获取有效数据块长度
        /// </summary>
        public int PayloadLength
        {
            get
            {
                if (Data == null) return 0;
                else return Data.Length;
            }
        }
        #endregion

        #region 获取或设置有效数据块内容
        /// <summary>
        /// 获取或设置有效数据块内容
        /// </summary>
        public byte[] Payload
        {
            set
            {
                if (value != null)
                    Data = value;
            }
            get
            {
                return Data;
            }
        }
        #endregion

        #region 设置有效数据字符串
        /// <summary>
        /// 设置有效数据字符串
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public bool SetPayloadString(string s)
        {
            if (s == null || s == string.Empty)
                return false;

            Payload = Encoding.UTF8.GetBytes(s);
            return true;
        }
        #endregion

        #region 获取或设置有效数据块内容
        /// <summary>
        /// 获取或设置有效数据块内容
        /// </summary>
        public string GetPayloadString()
        {
            if (Data == null || Data.Length == 0)
                return "";
            else
                return Encoding.UTF8.GetString(Payload);
        }
        #endregion

        #region 获取消息字节数组
        /// <summary>
        /// 获得消息字节数组
        /// </summary>
        public byte[] ToArray()
        {
            if (Data == null || Data.Length == 0)
                return BaseData;
            else
            {
                return BaseData.Concat(Data).ToArray();
               // return Factory.Combine(BaseData, Data).ToArray();
            }
        }
        #endregion
    }
}
